package com.gxar.marketdelivery.domain.repo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxar.marketdelivery.infra.dao.entity.DeliveryResSlotEntity;
import com.gxar.marketdelivery.infra.dao.mapper.DeliveryResSlotMapper;
import com.gxar.marketdelivery.interfaces.web.param.QueryDeliveryResSlotParam;
import com.gxar.marketdelivery.interfaces.web.result.IResponseStatusMsg;
import com.gxar.marketdelivery.shared.consts.DBConsts;
import com.gxar.marketdelivery.shared.utils.AssertUtils;
import com.gxar.marketdelivery.shared.utils.CollectionUtils;
import com.gxar.marketdelivery.shared.utils.DBUtils;
import com.gxar.marketdelivery.shared.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeliveryResSlotRepository {

    @Autowired
    private DeliveryResSlotMapper mapper;

    public void create(DeliveryResSlotEntity entity) {
        int ret = mapper.insert(entity);
        AssertUtils.isTrue(DBUtils.isSuccess(ret), IResponseStatusMsg.APIEnum.DB_ERROR, "新增资源位失败");
    }

    public void updateNotNull(DeliveryResSlotEntity entity) {
        int ret = mapper.updateById(entity);
        AssertUtils.isTrue(DBUtils.isSuccess(ret), IResponseStatusMsg.APIEnum.DB_ERROR, "更新资源位失败");
    }

    public DeliveryResSlotEntity getById(Long id) {
        if (id == null) {
            return null;
        }
        QueryWrapper<DeliveryResSlotEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryResSlotEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        queryWrapper.eq(DeliveryResSlotEntity.FIELD_ID, id);
        return mapper.selectOne(queryWrapper);
    }

    public List<DeliveryResSlotEntity> getByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }
        QueryWrapper<DeliveryResSlotEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryResSlotEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        queryWrapper.in(DeliveryResSlotEntity.FIELD_ID, ids);
        return mapper.selectList(queryWrapper);
    }

    public Map<Long, String> getResSlotKeyMap(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyMap();
        }
        List<DeliveryResSlotEntity> list = mapper.selectBatchIds(ids);
        return list.stream()
                .collect(Collectors.toMap(DeliveryResSlotEntity::getId, DeliveryResSlotEntity::getResSlotKey));
    }

    public DeliveryResSlotEntity getByKey(String resSlotKey) {
        if (resSlotKey == null) {
            return null;
        }
        LambdaQueryWrapper<DeliveryResSlotEntity> eq1 = new LambdaQueryWrapper<DeliveryResSlotEntity>().eq(
                        DeliveryResSlotEntity::getStatus, DBConsts.STATUS_AVAILABLE)
                .eq(DeliveryResSlotEntity::getResSlotKey, resSlotKey);
        return mapper.selectOne(eq1);
    }

    public List<DeliveryResSlotEntity> getByKeys(List<String> resSlotKeys) {
        if (CollectionUtils.isEmpty(resSlotKeys)) {
            return Collections.emptyList();
        }
        QueryWrapper<DeliveryResSlotEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryResSlotEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        queryWrapper.in(DeliveryResSlotEntity.FIELD_RES_SLOT_KEY, resSlotKeys);
        return mapper.selectList(queryWrapper);
    }

    public Integer getPageCount(QueryDeliveryResSlotParam param) {
        QueryWrapper<DeliveryResSlotEntity> queryWrapper = buildQuery(param);
        Long cnt = mapper.selectCount(queryWrapper);
        return cnt == null ? 0 : cnt.intValue();
    }

    public List<DeliveryResSlotEntity> getPageList(QueryDeliveryResSlotParam param) {
        QueryWrapper<DeliveryResSlotEntity> queryWrapper = buildQuery(param);
        queryWrapper.orderByDesc(DeliveryResSlotEntity.FIELD_GMT_MODIFY);
        List<DeliveryResSlotEntity> list = mapper.selectPage(new Page<>(param.getPageNow(), param.getPageSize()),
                queryWrapper).getRecords();
        return CollectionUtils.isEmpty(list) ? Collections.emptyList() : list;
    }

    private QueryWrapper<DeliveryResSlotEntity> buildQuery(QueryDeliveryResSlotParam param) {
        QueryWrapper<DeliveryResSlotEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(param.getResSlotName())) {
            queryWrapper.like(DeliveryResSlotEntity.FIELD_RES_SLOT_NAME, "%" + param.getResSlotName() + "%");
        }
        if (param.getResSlotStatus() != null) {
            queryWrapper.eq(DeliveryResSlotEntity.FIELD_RES_SLOT_STATUS, param.getResSlotStatus());
        }
        if (StringUtils.isNotBlank(param.getId())) {
            queryWrapper.eq(DeliveryResSlotEntity.FIELD_ID, Long.valueOf(param.getId()));
        }
        if (StringUtils.isNotEmpty(param.getResSlotKey())) {
            queryWrapper.like(DeliveryResSlotEntity.FIELD_RES_SLOT_KEY, "%" + param.getResSlotKey() + "%");
        }
        if (CollectionUtils.isNotEmpty(param.getCreators())) {
            queryWrapper.in(DeliveryResSlotEntity.FIELD_CREATOR, param.getCreators());
        }
        if (StringUtils.isNotBlank(param.getMinId())) {
            queryWrapper.lt(DeliveryResSlotEntity.FIELD_ID, Long.valueOf(param.getMinId()));
        }
        return queryWrapper;
    }
}
